/* v-backTop 回到顶部 */
function handleClick(target) {
	function handle() {
		target.scrollTo({
			top: 0,
			behavior: "smooth",
		});
	}
	return handle;
}
function scrollFn(el, binding) {
	function scroll(e) {
		if (e.srcElement.scrollTop > binding.value) {
			el.style.visibility = "unset";
		} else {
			el.style.visibility = "hidden";
		}
	}
	return scroll;
}

export default {
	inserted(el, binding, vnode) {
		// 指定元素
		const target = binding.arg ? document.getElementById(binding.arg) : window;
		el.addEventListener("click", handleClick(target));

		if (binding.value) {
			target.addEventListener("scroll", scrollFn(el, binding));
		}
		// 判断初始化状态
		if (target.scrollTop < binding.value) {
			el.style.visibility = "hidden";
		}
	},
	unbind(el, binding) {
		const target = binding.arg ? document.getElementById(binding.arg) : window;
		el.removeEventListener("click", handleClick(target));
		target.removeEventListener("scroll", scrollFn(el, binding));
	},
};
